<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-form-item label="卡片编号" prop="code">
        <el-input v-model="formItem.code" placeholder="请输入卡片编号" />
      </el-form-item>
      <el-form-item label="财管资产数量" prop="amount">
        <el-input-number v-model="formItem.amount" :precision="0" :step="1" :max="1000000000" />
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input v-model="formItem.description" type="textarea" :rows="3" placeholder="请输入说明" />
      </el-form-item>
      <el-form-item label="" prop="description">
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="assetDialogVisible = true">添加资产</el-button>
      </el-form-item>
      <el-form-item label="资产：">
        <el-table
          :loading="false"
          :data="formItem.assets"
          class="fixed-table-table"
        >
          <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :prop="column.key" :sortable="column.sortable">
            <template slot-scope="scope">
              <column-render :render="column.render" :row="scope.row" :column="column" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed="right">
            <template slot-scope="{row}">
              <el-button type="text" size="mini" icon="el-icon-delete" :style="{color: row.dirty ? 'red' : ''}" title="移除" @click="remove(row)" />
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <SelectDialog title="选择资产" :append-to-body="true" :show="assetDialogVisible" @on-ok="onSelectedAsset" @on-cancel="assetDialogVisible = false">
      <AssetTable
        v-if="assetDialogVisible"
        ref="assets"
        belongs="sl"
        :for-selection="true"
        categories="1"
      />
    </SelectDialog>
  </div>
</template>

<script>
import SelectDialog from '@neu/biz-core/components/SelectDialog'
import AssetTable from '@neu/dcam-ui/views/fixedAsset/tables'
import {
  create,
  update,
  detail,
  associate
} from '@neu/dcam-ui/api/ahassetcard'

// 定义一个表单原始项
const formItem = {
  code: '',
  amount: 0,
  description: '',
  assets: []
}
export default {
  name: 'AhAssetCardEditForm',
  components: {
    SelectDialog,
    AssetTable
  },
  props: {
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      formItem: {
        ...formItem
      },
      insideColumns: [
        { title: '资产编号', width: 100, key: 'code' },
        { title: '资产名', width: 100, key: 'name' },
        { title: '大分类', width: 100, key: 'bigTypeName' },
        { title: '中分类', width: 100, key: 'middleTypeName' },
        { title: '小分类', width: 100, key: 'smallTypeName' },
        { title: '规格型号', width: 100, key: 'specifications' }
      ],
      // 表单验证规则
      rules: {
        code: [
          { required: true, message: '卡片编号不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ]
      },
      assetDialogVisible: false
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              update(this.id, this.formItem).then(response => {
                resolve(associate(response.data.id, this.formItem.assets.map(it => it.id)))
              })
              // resolve(update(this.id, this.formItem))
            } else {
              create(this.formItem).then(response => {
                resolve(associate(response.data.id, this.formItem.assets.map(it => it.id)))
              })
              // resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    },
    onSelectedAsset() {
      let assets = this.$refs.assets.selections
      assets = assets.filter(it => this.formItem.assets.filter(o => o.id === it.id).length === 0)
      assets.forEach(it => {
        it.dirty = true
      })
      this.formItem.assets = [...this.formItem.assets, ...assets]
      this.assetDialogVisible = false
    },
    remove(row) {
      this.formItem.assets = this.formItem.assets.filter(it => it.id !== row.id)
    }
  }
}
</script>
